// pages/newAddress/index.js
const {
  get,
  post
} = getApp();
Page({
  data: {
    params: {
      name: "",
      province_id: "",
      city_id: "",
      area_id: "",
      province: "",
      city: "",
      area: "",
      addr: "",
      mobile: "",
      def_addr: 0
    },
    show: false,
    columns: [{
        values: [],
        data: [],
        defaultIndex: 0
      },
      {
        values: [],
        data: [],
        defaultIndex: 0
      },
      {
        values: [],
        data: [],
        defaultIndex: 0
      }
    ]
  },
  // 开启地址选择
  openPicker() {
    this.setData({
      show: true
    });
    this.getCityList(true);
  },
  // 确定选择
  changeConfirm(e) {
    this.setData({
      "params.province": e.detail.value[0],
      "params.province_id": this.data.columns[0].data[e.detail.index[0]].city_list_id,
      "params.city": e.detail.value[1],
      "params.city_id": this.data.columns[1].data[e.detail.index[1]].city_list_id,
      "params.area": e.detail.value[2],
      "params.area_id": this.data.columns[2].data[e.detail.index[2]].city_list_id,
      "show": false
    });
  },
  // 改变
  onChange(e) {
    const {
      index
    } = e.detail
    if (index == 0) {
      this.setData({
        [`columns[1].values`]: [],
        [`columns[1].data`]: [],
        [`columns[1].defaultIndex`]: 0,
        [`columns[2].values`]: [],
        [`columns[2].data`]: [],
        [`columns[2].defaultIndex`]: 0
      });
      this.data.columns[0].data.map(item => {
        if (e.detail.value[0] == item.name) {
          this.setData({
            [`columns[1].data`]: item.children,
            [`columns[1].values`]: item.children.map(item => item.name)
          });
          this.getAreaList(item.children[0].city_list_id);
        }
      });
    } else if (index == 1) {
      this.setData({
        [`columns[2].values`]: [],
        [`columns[2].data`]: [],
        [`columns[2].defaultIndex`]: 0,
      })
      this.data.columns[1].data.map(item => {
        if (e.detail.value[1] == item.name) {
          this.setData({
            [`columns[2].values`]: item.children.map(item => item.name),
            [`columns[2].data`]: item.children
          })
          this.getAreaList(item.city_list_id);
        }
      });
    }
  },
  // 点击取消
  changeCancel() {
    this.setData({
      show: false
    });
  },
  // 初始化加载
  onLoad: function (e) {
    // 编辑
    if (e.hasOwnProperty("id")) {
      this.getAddress(e.id);
    };
    this.getCityList(false);
  },
  // 获取地址列表
  getCityList(e) {
    if (e) {
      this.data.columns[0].data.map((item, index) => {
        if (this.data.params.province_id == item.city_list_id) {
          this.setData({
            [`columns[0].defaultIndex`]: index,
            [`columns[1].data`]: this.data.columns[0].data[index].children,
            [`columns[1].values`]: this.data.columns[0].data[index].children.map(
              item => item.name
            )
          })
          item.children.map((item1, index1) => {
            if (this.data.params.city_id == item1.city_list_id) {
              this.setData({
                [`columns[1].defaultIndex`]: index1
              })
              this.getAreaList(item1.city_list_id);
            }
          });
        }
      });
    } else {
      get("/address/province-city").then(res => {
        if (res.result == 1) {
          this.setData({
            [`columns[0].data`]: res.data,
            [`columns[0].values`]: res.data.map(item => item.name),
            [`columns[1].data`]: res.data[0].children,
            [`columns[1].values`]: res.data[0].children.map(item => item.name)
          });
          this.getAreaList(res.data[0].children[0].city_list_id);
        };
      })
    }
  },
  // 获取地区
  getAreaList(e) {
    get(`/address/area`, {
      p_city_list_id: e
    }).then(res => {
      if (res.result == 1) {
        this.setData({
          [`columns[2].data`]: res.data,
          [`columns[2].values`]: res.data.map(item => item.name)
        });
      }
      res.data.map((item, index) => {
        if (this.data.params.area_id == item.city_list_id) {
          this.setData({
            [`columns[2].defaultIndex`]: index
          });
        }
      });
    });
  },
  // 新增编辑
  preservation() {
    if (this.data.params.name == "") {
      wx.showToast({
        title: '收货姓名错误',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (!/^1[3456789]\d{9}$/.test(this.data.params.mobile)) {
      wx.showToast({
        title: '手机号码错误',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (this.data.params.province_id == "" | this.data.params.city_id == "" | this.data.params.area_id == "") {
      wx.showToast({
        title: '收货地区错误',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (this.data.params.addr == "") {
      wx.showToast({
        title: '收货地址错误',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (this.data.params.hasOwnProperty("id")) {
      post("/address/update", this.data.params).then(res => {
        if (res.result == 1) {
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 2000
          });
          wx.navigateBack({
            delta: 1
          });
        }
      });
    } else {
      post("/address/save", this.data.params).then(res => {
        if (res.result == 1) {
          wx.showToast({
            title: '保存成功',
            icon: 'success',
            duration: 2000
          });
          wx.navigateBack({
            delta: 1
          });
        }
      });

    }
  },
  // 改名字
  changeName(e) {
    this.setData({
      "params.name": e.detail
    });
  },
  // 改电话
  changeMobile(e) {
    this.setData({
      "params.mobile": e.detail
    });
  },
  // 改地址
  changeAddr(e) {
    this.setData({
      "params.addr": e.detail
    });
  },
  // 改变默认
  changeAddress(e) {
    // 需要手动对 checked 状态进行更新
    console.log(e);
    this.setData({
      "params.def_addr": e.detail ? 1 : 0
    });
  },
  // 获取买家收货地址
  getAddress(e) {
    get("/address/details", {
      addr_id: e
    }).then(res => {
      if (res.result == 1) {
        this.setData({
          params: res.data
        });
      }
    })
  },
  onShow: function () {
    // 控制分享按钮
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline'],
      success(res) {
        console.log('showShareMenu', res);
      }
    });
  }
})